var app = getApp()
Page({
  data: {
    //判断小程序的API，回调，参数，组件等是否在当前版本可用。
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  onLoad: function (options) {
    var that = this;
    console.log('从积攒详情来有options')
    console.log(options)
    // 查看是否授权
    if(options.d_id){
      that.setData({
        d_id:options.d_id,
        product_id: options.product_id
      })
    }
    console.log(getCurrentPages())
    var route = getCurrentPages()[0].route;
    route = route.replace("pages","..");
    console.log(route)
    that.setData({
      route:route
    })
    wx.login({
      success: function (login) {
        console.log(login)
        that.setData({
          code :login.code
        })

      }
    })
    wx.getSetting({
      success: function (res) {
        if (res.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success: function (res) {
              //从数据库获取用户信息
              console.log(res)
             if(app.globalData.opid == ""){
               that.queryUsreInfo();
               
             }else{
               //用户已经授权过
               if(that.data.d_id){
                 wx.reLaunch({
                   url: that.data.route + '?d_id=' + that.data.d_id + '&id=' + that.data.product_id
                 })
               }else{
                 wx.reLaunch({
                   url: that.data.route
                 })
               }
                 
             }
            }
          });
        }
      }
    })
  },
  bindGetUserInfo: function (e) {
    if (e.detail.userInfo) {
      wx.showLoading({
        title: '信息加载中',
      })
      //用户按了允许授权按钮
      var that = this;
      //插入登录的用户的相关信息到数据库
      console.log('登录页面：')
      console.log(app.globalData)
      wx.request({
        url: app.globalData.url + 'wxlogin.php',
        data: {
          name:
            JSON.stringify({
              code: that.data.code,
              user: e.detail.userInfo,
              openid: app.globalData.shareopenid
            })
        },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          //从数据库获取用户信息
          console.log(res)
          console.log(app.globalData)

         if(res.data){
           app.globalData.opid = res.data.openid;
           app.globalData.userInfo = e.detail.userInfo;
           console.log("插入小程序登录用户信息成功！");
           console.log(app.globalData.userInfo)
           wx.hideLoading()
           wx.showToast({
             title: '授权成功',
             icon: 'none'
           })
           if (that.data.d_id) {
             console.log('返回带参的路径')
             console.log(that.data.route + '?d_id=' + that.data.d_id + '&id=' + that.data.product_id)
             wx.reLaunch({
               url: that.data.route + '?d_id=' + that.data.d_id + '&id=' + that.data.product_id
             })
           } else {
             wx.reLaunch({
               url: that.data.route
             })
           }
         }else{
           wx.showToast({
             title: '授权失败，请重新授权',
             icon:'none'
           })
         }
          
         
        }
      });
     
    } else {
      //用户按了拒绝按钮
      wx.showModal({
        title: '警告',
        content: '您点击了拒绝授权，将无法进入小程序，请授权之后再进入!!!',
        showCancel: false,
        confirmText: '返回授权',
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击了“返回授权”')
          }
        }
      })
    }
  },
  //获取用户信息接口
  queryUsreInfo: function () {
    var that = this;
    wx.login({
      success: function (login) {
        that.globalData.code = login.code
        wx.request({
          url: app.globalData.url + 'wxlogin.php',
          data: {
            openid: app.globalData.openid,
            code: login.code

          },
          header: {
            'content-type': 'application/json'
          },
          success: function (res) {
            console.log(res.data);
            if (res.data) {
              app.globalData.opid = res.data.openid;
              app.globalData.userInfo = e.detail.userInfo;

            }
            if (that.data.d_id) {
              wx.reLaunch({
                url: that.data.route + '?d_id=' + that.data.d_id + '&id=' + that.data.product_id
              })
            } else {
              wx.reLaunch({
                url: that.data.route
              })
            }
          }
        }) 

      }
    })
   
  },

})
